//
//  FilterViewController.m
//  FMS
//
//  Created by Steven on 10/13/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "FilterViewController.h"
#import "MaintenanceMgmtViewController.h"
#import "AddNewItemViewController.h"
#import "LiaisonBaseLite.h"
#import "BuildingMgmtViewController.h"

@interface FilterViewController ()

@end

@implementation FilterViewController
@synthesize delegate;
@synthesize currentSelectedIndex;
@synthesize buildingId;
@synthesize serviceId;
@synthesize campId;
@synthesize unitId;
@synthesize systemName;
@synthesize filterValueDict;
@synthesize itemType;

- (id)initWithStyle:(UITableViewStyle)style filterType:(int)_filterType
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
        filterType = _filterType;
    }
    return self;
}

- (NSMutableArray*) listLiaison {
    return [LiaisonBaseLite queryListsInDatabase:[CouchBaseLiteLib sharedCouchBaseLite].database];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    mainArray = [NSMutableArray arrayWithCapacity:1];
    
    myActivityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(160, 160, 25, 25)];
    myActivityView.hidesWhenStopped = YES;
    myActivityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.view addSubview:myActivityView];
    [myActivityView startAnimating];

    [self delayListFilter];
}

- (void) delayListFilter {
//    dispatch_queue_t myQueue = dispatch_queue_create("com.listFilter", NULL);
//    dispatch_async(myQueue, ^(void) {
        [self listFilter];
//        dispatch_async(dispatch_get_main_queue(), ^(void) {
            if ([mainArray count] * 44 > 480) {
                [self setContentSizeForViewInPopover:CGSizeMake(320, 480)];
            }
            else
                [self setContentSizeForViewInPopover:CGSizeMake(320, [mainArray count] * 44)];
            [self.tableView reloadData];
            [myActivityView stopAnimating];
//        });
//    });
}

- (void) listFilter {
    [mainArray removeAllObjects];
    currentRecordIndex = 0;
    
    NSMutableDictionary *filterDict;
    NSMutableArray *tmpArray;
    switch (filterType) {
        case filter_service:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-service"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-service.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            tmpArray = [CouchBaseLiteLib listService];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            
            break;
        case filter_camp:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-camp"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-camp.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
                    
            tmpArray = [CouchBaseLiteLib listCampByService:serviceId];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            
            break;
        case filter_unit:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-unit"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-unit.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
                
            tmpArray = [CouchBaseLiteLib listUnitByService:serviceId andCamp:campId];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            break;
        case filter_building:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-building"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-building.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            if (itemType != Building_Type && (![unitId isEqualToString:@"0"] && unitId != Nil)) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"-1",@"index",[dictLanguage objectForKey:@"filter.select-building.other"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            tmpArray = [CouchBaseLiteLib listBuildingByUnit:unitId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            
            currentRecordIndex += [tmpArray count];
            
            if ([tmpArray count] == NUMBER_ROW_PAGE) {
                isLoadMore = YES;
            }
            else {
                isLoadMore = NO;
            }
            
            [self.tableView reloadData];
            
            isLock = NO;
            
            break;
        case filter_location:
            tmpArray = [CouchBaseLiteLib listOtherBuildingByUnit:unitId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            
            currentRecordIndex += [tmpArray count];
            
            if ([tmpArray count] == NUMBER_ROW_PAGE) {
                isLoadMore = YES;
            }
            else {
                isLoadMore = NO;
            }
            
            [self.tableView reloadData];
            
            isLock = NO;
            break;
        case filter_floor:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-floor"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-floor.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            tmpArray = [CouchBaseLiteLib listFloorsByBuilding:buildingId];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            break;
        case filter_room:
        {
            NSMutableArray *tmpArray;
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
            if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]) {
                tmpArray = [CouchBaseLiteLib listRoomByFilter:filterValueDict from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            }
            else {
                NSString *contractorId = [userDefaults objectForKey:ACCOUNT_ID];
                tmpArray = [CouchBaseLiteLib listRoomByFilter:filterValueDict contractor:contractorId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            }
            
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                NSString *_id = [dict objectForKey:@"_id"];
                NSString *_name = [dict objectForKey:@"item_name"];
                
                NSMutableDictionary *roomDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:_id,@"index",_name,@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:roomDict];
            }
            
            currentRecordIndex += [tmpArray count];
            
            if ([tmpArray count] == NUMBER_ROW_PAGE) {
                isLoadMore = YES;
            }
            else {
                isLoadMore = NO;
            }
            
            [self.tableView reloadData];
            
            isLock = NO;
            
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",0],@"index",[dictLanguage objectForKey:@"filter.select-room"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray insertObject:filterDict atIndex:0];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",0],@"index",[dictLanguage objectForKey:@"filter.select-room.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray insertObject:filterDict atIndex:1];
            }
            break;
        }
        case filter_system:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-system"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-system.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            tmpArray = [CouchBaseLiteLib listSystemByFilter:filterValueDict];
            
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            break;
        case filter_description:
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-description"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            //if add new or edit item view, dont add select all value
            if (![delegate isKindOfClass:[AddNewItemViewController class]]) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"filter.select-description.all"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            tmpArray = [CouchBaseLiteLib listDescriptionBySystem:systemName];
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                [mainArray addObject:dict];
            }
            break;
        case filter_office:
        {
            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"schedule.title-select"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray addObject:filterDict];
            
            NSString *typeStr[] = {
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-accommodation"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-office"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-store"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-building"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-external-area"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-training-area"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-special-facilities"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-infrastructures"]
            };
            
            for (int i=0;i < sizeof(typeStr)/sizeof(typeStr[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",i+1],@"index",typeStr[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            break;
        }
        case filter_normal_ops:
        {
//            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"index",[dictLanguage objectForKey:@"schedule.title-select"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
//            [mainArray addObject:filterDict];
            
            NSString *typeStr[] = {
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-normal"],
                [dictLanguage objectForKey:@"building-mgmt.add-new-room.type-ops-critical"]
            };
            
            for (int i=0;i < sizeof(typeStr)/sizeof(typeStr[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",i+1],@"index",typeStr[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            break;
        }
        case filter_contractor:
        {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
            NSString *contractorId = [userDefaults objectForKey:ACCOUNT_ID];
            if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]) {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContact:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }
            else {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContactByUser:contractorId from:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }

            for (NSMutableDictionary *dict in tmpArray) {
                [dict setObject:[dict objectForKey:@"_id"] forKey:@"index"];
                [dict setObject:[NSNumber numberWithInt:filterType] forKey:@"filterType"];
                [dict setObject:[dict objectForKey:@"personInCharge"] == nil ? [dict objectForKey:@"name"] : [dict objectForKey:@"personInCharge"] forKey:@"filterName"];
                [dict setObject:[dict objectForKey:@"Table"] forKey:@"Table"];
                [dict removeObjectForKey:@"personInCharge"];
                
                [mainArray addObject:dict];
            }

            break;
        }
        case filter_person_in_charge:
        {
#ifndef vDEBUG
            NSMutableArray *tmpArray;
            //MA và Owner view all equipment. Contractor chỉ view được những equipment mà nó add
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
            NSString *contractorId = [userDefaults objectForKey:ACCOUNT_ID];
            if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]) {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContact:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }
            else {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContactByUser:contractorId from:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }
            
            for (NSMutableDictionary *dict in tmpArray) {
                [dict setObject:[dict objectForKey:@"_id"] forKey:@"index"];
                [dict setObject:[NSNumber numberWithInt:filterType] forKey:@"filterType"];
                [dict setObject:[dict objectForKey:@"personInCharge"] == nil ? [dict objectForKey:@"name"] : [dict objectForKey:@"personInCharge"] forKey:@"filterName"];
                [dict removeObjectForKey:@"personInCharge"];
                
                [mainArray addObject:dict];
            }
            
            currentRecordIndex += [tmpArray count];
            
            if ([tmpArray count] == NUMBER_ROW_PAGE) {
                isLoadMore = YES;
            }
            else {
                isLoadMore = NO;
            }
            
            [self.tableView reloadData];
            
            isLock = NO;
#else
        {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"index",[NSNumber numberWithInt:filterType],@"filterType",@"vinh",@"filterName",nil];
            [mainArray addObject:dict];
        }
#endif
        }
            break;
        case filter_item_type:
        {
            NSString *itemTypeStr[] = {
                [dictLanguage objectForKey:@"schedule.title-building"],
                [dictLanguage objectForKey:@"schedule.title-facility"]
            };
            for (int i=0;i < 2;i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",i+1],@"index",itemTypeStr[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            break;
        }
        case filter_fsr:
        {
            NSString *itemTypeStr[] = {@"FSR",@"Non FSR"};
            for (int i=0;i < 2;i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",i],@"index",itemTypeStr[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            break;
        }
        case filter_maintenance:
        {
            NSString *firstElement = @"";
            if ([delegate isKindOfClass:[MaintenanceMgmtViewController class]])
                firstElement = [dictLanguage objectForKey:@"schedule.title-all"];
            else if ([delegate isKindOfClass:[BuildingMgmtViewController class]])
                firstElement = [dictLanguage objectForKey:@"schedule.title-select-work-type"];
            else
                firstElement = [dictLanguage objectForKey:@"maintenance-mgmt.add-new.select-maintenance-type"];
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
            NSString *subItemName[] = {
                firstElement,
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.pmws"],
                [dictLanguage objectForKey:@"schedule.filter-maintenance-type-pm-work"],
                [dictLanguage objectForKey:@"schedule.filter-maintenance-type-cm-work"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.aws"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.pms"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.cm-above-cost-cap"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.cm-not-under-pmws"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.new-works-above-70"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.new-works-below-70"],
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",PMWS],
                [NSString stringWithFormat:@"%d",PM],
                [NSString stringWithFormat:@"%d",CM],
                [NSString stringWithFormat:@"%d",AWS],
                [NSString stringWithFormat:@"%d",PMS],
                [NSString stringWithFormat:@"%d",CM_Above_Cost_Cap],
                [NSString stringWithFormat:@"%d",CM_Not_Under_PMWS],
                [NSString stringWithFormat:@"%d",New_Works_Above_70],
                [NSString stringWithFormat:@"%d",New_Works_Below_70],
            };
            NSString *level[] = {
                @"0",
                @"0",
                @"1",
                @"1",
                @"0",
                @"1",
                @"1",
                @"1",
                @"1",
                @"1",
            };
            
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType",level[i],@"level", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
        case filter_liaison:
        {
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
//            NSString *subItemName[] = {
//                [dictLanguage objectForKey:@"liaison.title-select-type"],
//                [dictLanguage objectForKey:@"liaison.title-ma"],
//                [dictLanguage objectForKey:@"liaison.title-contractor"],
//                [dictLanguage objectForKey:@"liaison.title-sub-contractor"],
//                [dictLanguage objectForKey:@"liaison.title-owner"],
//                [dictLanguage objectForKey:@"liaison.title-other"],
//
//            };
//            NSString *maintenanceType[] = {
//                [NSString stringWithFormat:@"%d",0],
//                [NSString stringWithFormat:@"%d",MA],
//                [NSString stringWithFormat:@"%d",CONTRUCTOR],
//                [NSString stringWithFormat:@"%d",SUB_CONTRUCTOR],
//                [NSString stringWithFormat:@"%d",OWNER],
//                [NSString stringWithFormat:@"%d",OTHER],
//            };
//            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
//                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
//                [mainArray addObject:filterDict];
//            }
            
            NSMutableArray *roleArray = [[CouchBaseLiteLib sharedCouchBaseLite] listAllRole];
            for (int i=0;i < [roleArray count];i++) {
//                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//                NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
                NSMutableDictionary *dict = [roleArray objectAtIndex:i];
                
                if ([[[dict objectForKey:@"roleName"] lowercaseString] isEqualToString:@"admin"])
                    ;
                else{
                    filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"_id"],@"index",[dict objectForKey:@"roleName"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                    [mainArray addObject:filterDict];
//                    if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]){
//                        
//                        filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"_id"],@"index",[dict objectForKey:@"roleName"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
//                        [mainArray addObject:filterDict];
//                    }
//                    else{
//                        if ([[[dict objectForKey:@"roleName"] lowercaseString] isEqualToString:@"admin"] || [[[dict objectForKey:@"roleName"] lowercaseString] isEqualToString:@"ma"] || [[[dict objectForKey:@"roleName"] lowercaseString] isEqualToString:@"owner"])
//                            ;
//                        else{
//                            filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"_id"],@"index",[dict objectForKey:@"roleName"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
//                            [mainArray addObject:filterDict];
//                        }
//                    }
                }

            }
            NSMutableDictionary *dictTitle = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dictLanguage objectForKey:@"liaison.title-select-type"],@"filterName",@"0",@"index",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray insertObject:dictTitle atIndex:0];
            break;
        }
        case filter_category:
        {
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
            NSString *subItemName[] = {
                [dictLanguage objectForKey:@"schedule.title-select-category-program"],
                [dictLanguage objectForKey:@"maintenance-mgmt.service-type.inspection"],
                [dictLanguage objectForKey:@"maintenance-mgmt.service-type.maintenance"],
                
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",Inspection],
                [NSString stringWithFormat:@"%d",Maintenance],
            };
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
        case filter_inspection:
        {
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
            NSString *subItemName[] = {
                [dictLanguage objectForKey:@"schedule.title-select"],
                [dictLanguage objectForKey:@"schedule.title-monthly"],
                [dictLanguage objectForKey:@"schedule.title-quaterly"],
                [dictLanguage objectForKey:@"schedule.title-yearly"],
                [dictLanguage objectForKey:@"schedule.title-two-yearly"],
                [dictLanguage objectForKey:@"schedule.title-five-yearly"],
                [dictLanguage objectForKey:@"schedule.title-other"],
                
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",MONTHLY],
                [NSString stringWithFormat:@"%d",QUATERLY],
                [NSString stringWithFormat:@"%d",YEARLY],
                [NSString stringWithFormat:@"%d",TWO_YEARLY],
                [NSString stringWithFormat:@"%d",FIVE_YEARLY],
                [NSString stringWithFormat:@"%d",OTHER],
            };
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
        case filter_scheduleCategory:
        {
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
            NSString *subItemName[] = {
                [dictLanguage objectForKey:@"schedule.title-all"],
                [dictLanguage objectForKey:@"maintenance-mgmt.service-type.inspection"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.pmws"],
                [dictLanguage objectForKey:@"schedule.filter-maintenance-type-pm-work"],
                [dictLanguage objectForKey:@"schedule.filter-maintenance-type-cm-work"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.aws"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.pms"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.cm-above-cost-cap"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.cm-not-under-pmws"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.new-works-above-70"],
                [dictLanguage objectForKey:@"maintenance-mgmt.maintenance-type.new-works-below-70"],
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",Inspection_Category_Type],
                [NSString stringWithFormat:@"%d",PMWS],
                [NSString stringWithFormat:@"%d",PM],
                [NSString stringWithFormat:@"%d",CM],
                [NSString stringWithFormat:@"%d",AWS],
                [NSString stringWithFormat:@"%d",PMS],
                [NSString stringWithFormat:@"%d",CM_Above_Cost_Cap],
                [NSString stringWithFormat:@"%d",CM_Not_Under_PMWS],
                [NSString stringWithFormat:@"%d",New_Works_Above_70],
                [NSString stringWithFormat:@"%d",New_Works_Below_70],
            };
            
            NSString *level[] = {
                @"0",
                @"0",
                @"0",
                @"1",
                @"1",
                @"0",
                @"1",
                @"1",
                @"1",
                @"1",
                @"1",
            };
            
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType",level[i],@"level", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
        case filter_filter_liaison:
        {
            /************ init value for PMWS *************/
            //add 2 objects: PM & CM below cost cap to PMWS
//            NSString *subItemName[] = {
//                [dictLanguage objectForKey:@"liaison.title-filter-by"],
//                [dictLanguage objectForKey:@"liaison.title-ma"],
//                [dictLanguage objectForKey:@"liaison.title-contractor"],
//                [dictLanguage objectForKey:@"liaison.title-sub-contractor"],
//                [dictLanguage objectForKey:@"liaison.title-owner"],
//                [dictLanguage objectForKey:@"liaison.title-other"],
//                
//            };
//            NSString *maintenanceType[] = {
//                [NSString stringWithFormat:@"%d",0],
//                [NSString stringWithFormat:@"%d",MA],
//                [NSString stringWithFormat:@"%d",CONTRUCTOR],
//                [NSString stringWithFormat:@"%d",SUB_CONTRUCTOR],
//                [NSString stringWithFormat:@"%d",OWNER],
//                [NSString stringWithFormat:@"%d",OTHER],
//            };
//            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
//                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
//                [mainArray addObject:filterDict];
//            }
            NSMutableArray *roleArray = [[CouchBaseLiteLib sharedCouchBaseLite] listAllRole];
            
            for (int i=0;i < [roleArray count];i++) {
                NSMutableDictionary *dict = [roleArray objectAtIndex:i];
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"_id"],@"index",[dict objectForKey:@"roleName"],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            NSMutableDictionary *dictTitle = [NSMutableDictionary dictionaryWithObjectsAndKeys:[dictLanguage objectForKey:@"liaison.title-filter-by"],@"filterName",@"0",@"index",[NSNumber numberWithInteger:filterType],@"filterType", nil];
            [mainArray insertObject:dictTitle atIndex:0];
            
            break;
        }
        case filter_sort_liaison:
        {
            /************ init value for Liaison *************/

            NSString *subItemName[] = {
                [dictLanguage objectForKey:@"liaison.title-sort-by"],
                [dictLanguage objectForKey:@"liaison.title-company"],
                [dictLanguage objectForKey:@"liaison.title-contact-type"],
                [dictLanguage objectForKey:@"liaison.title-person-in-charge"],
                
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",COMPANY],
                [NSString stringWithFormat:@"%d",CONTACT_TYPE],
                [NSString stringWithFormat:@"%d",PERSON_IN_CHARGE],
            };
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
        case filter_work_type:
        {
            /************ init value for work type *************/

            NSString *subItemName[] = {
                [dictLanguage objectForKey:@"filter.select-item-type"],
                [dictLanguage objectForKey:@"schedule.title-building"],
                [dictLanguage objectForKey:@"schedule.title-facility"],
                
            };
            NSString *maintenanceType[] = {
                [NSString stringWithFormat:@"%d",0],
                [NSString stringWithFormat:@"%d",Building_Type],
                [NSString stringWithFormat:@"%d",Facility_Type],
            };
            for (int i=0;i < sizeof(subItemName)/sizeof(subItemName[0]);i++) {
                filterDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:maintenanceType[i],@"index",subItemName[i],@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:filterDict];
            }
            
            break;
        }
       
        default:
            break;
    }
}

- (void) listMoreRoom {
    if (!isLock) {
//        dispatch_queue_t myQueue = dispatch_queue_create("com.myqgs.listMoreContact", NULL);
//        dispatch_async(myQueue, ^(void) {
            isLock = YES;
            
            NSMutableArray *tmpArray;
            //MA và Owner view all equipment. Contractor chỉ view được những equipment mà nó add
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
            NSString *contractorId = [userDefaults objectForKey:ACCOUNT_ID];
            if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]) {
//                tmpArray = [CouchBaseLiteLib listRoomFrom:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
                tmpArray = [CouchBaseLiteLib listRoomByFilter:filterValueDict from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            }
            else {
//                tmpArray = [CouchBaseLiteLib listRoomByContractor:contractorId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
                tmpArray = [CouchBaseLiteLib listRoomByFilter:filterValueDict contractor:contractorId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            }
            
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                NSString *_id = [dict objectForKey:@"_id"];
                NSString *_name = [dict objectForKey:@"item_name"];
                
                NSMutableDictionary *roomDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:_id,@"index",_name,@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:roomDict];
            }
            
//            dispatch_async(dispatch_get_main_queue(), ^(void) {
                int previousIndex = currentRecordIndex;
                currentRecordIndex += [tmpArray count];
                
                if ([tmpArray count] == NUMBER_ROW_PAGE) {
                    isLoadMore = YES;
                }
                else {
                    isLoadMore = NO;
                }
                
                if (isLoadMore) {
                    //                    [self.mainTableView showLoadingMore];
                }
                else
                    [self.tableView hideLoadingMore];
                [self.tableView hideActivity];
                
                NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
                for (int i=previousIndex;i < currentRecordIndex;i++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    [indexPaths addObject:indexPath];
                }
                if ([indexPaths count] > 0) {
                    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                
                isLock = NO;
//            });
//        });
#if !OS_OBJECT_USE_OBJC
//        dispatch_release(myQueue);
#endif
//        myQueue = nil;
    }
}

- (void) listMoreContact {
    if (!isLock) {
//        dispatch_queue_t myQueue = dispatch_queue_create("com.myqgs.listMoreContact", NULL);
//        dispatch_async(myQueue, ^(void) {
            isLock = YES;
            
            NSMutableArray *tmpArray;
            //MA và Owner view all equipment. Contractor chỉ view được những equipment mà nó add
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *role = [[userDefaults objectForKey:ACCOUNT_ROLE] lowercaseString];
            NSString *contractorId = [userDefaults objectForKey:ACCOUNT_ID];
            if ([role isEqualToString:@"ma"] || [role isEqualToString:@"owner"] || [role isEqualToString:@"admin"]) {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContact:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }
            else {
                tmpArray = [CouchBaseLiteLib listUserLiasonAndContactByUser:contractorId from:currentRecordIndex numberPage:NUMBER_ROW_PAGE];
            }
            
            for (NSMutableDictionary *dict in tmpArray) {
                [dict setObject:[dict objectForKey:@"_id"] forKey:@"index"];
                [dict setObject:[NSNumber numberWithInt:filterType] forKey:@"filterType"];
                [dict setObject:[dict objectForKey:@"personInCharge"] == nil ? [dict objectForKey:@"name"] : [dict objectForKey:@"personInCharge"] forKey:@"filterName"];
                [dict removeObjectForKey:@"personInCharge"];
                [mainArray addObject:dict];
            }
//            dispatch_async(dispatch_get_main_queue(), ^(void) {
                int previousIndex = currentRecordIndex;
                currentRecordIndex += [tmpArray count];
                
                if ([tmpArray count] == NUMBER_ROW_PAGE) {
                    isLoadMore = YES;
                }
                else {
                    isLoadMore = NO;
                }
                
                if (isLoadMore) {
                    //                    [self.mainTableView showLoadingMore];
                }
                else
                    [self.tableView hideLoadingMore];
                [self.tableView hideActivity];
                
                NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
                for (int i=previousIndex;i < currentRecordIndex;i++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    [indexPaths addObject:indexPath];
                }
                if ([indexPaths count] > 0) {
                    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                
                isLock = NO;
//            });
//        });
#if !OS_OBJECT_USE_OBJC
//        dispatch_release(myQueue);
#endif
//        myQueue = nil;
    }
}

- (void) listMoreBuildings {
    if (!isLock) {
//        dispatch_queue_t myQueue = dispatch_queue_create("com.myqgs.listMoreBuilding", NULL);
//        dispatch_async(myQueue, ^(void) {
            isLock = YES;
            
            NSMutableArray *tmpArray = [CouchBaseLiteLib listBuildingByUnit:unitId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                NSString *_id = [dict objectForKey:@"_id"];
                NSString *_name = [dict objectForKey:@"item_name"];
                
                NSMutableDictionary *roomDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:_id,@"index",_name,@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:roomDict];
            }
            
//            dispatch_async(dispatch_get_main_queue(), ^(void) {
                int previousIndex = currentRecordIndex;
                currentRecordIndex += [tmpArray count];
                
                if ([tmpArray count] == NUMBER_ROW_PAGE) {
                    isLoadMore = YES;
                }
                else {
                    isLoadMore = NO;
                }
                
                if (isLoadMore) {
                    //                    [self.mainTableView showLoadingMore];
                }
                else
                    [self.tableView hideLoadingMore];
                [self.tableView hideActivity];
                
                NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
                for (int i=previousIndex;i < currentRecordIndex;i++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    [indexPaths addObject:indexPath];
                }
                if ([indexPaths count] > 0) {
                    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                
                isLock = NO;
//            });
//        });
#if !OS_OBJECT_USE_OBJC
//        dispatch_release(myQueue);
#endif
//        myQueue = nil;
    }
}

- (void) listMoreOtherBuildings {
    if (!isLock) {
//        dispatch_queue_t myQueue = dispatch_queue_create("com.myqgs.listMoreOtherBuildings", NULL);
//        dispatch_async(myQueue, ^(void) {
            isLock = YES;
            
            NSMutableArray *tmpArray = [CouchBaseLiteLib listOtherBuildingByUnit:unitId from:currentRecordIndex numberRow:NUMBER_ROW_PAGE];
            
            for (int i=0;i < [tmpArray count];i++) {
                NSMutableDictionary *dict = [tmpArray objectAtIndex:i];
                NSString *_id = [dict objectForKey:@"_id"];
                NSString *_name = [dict objectForKey:@"item_name"];
                
                NSMutableDictionary *roomDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:_id,@"index",_name,@"filterName",[NSNumber numberWithInteger:filterType],@"filterType", nil];
                [mainArray addObject:roomDict];
            }
            
//            dispatch_async(dispatch_get_main_queue(), ^(void) {
                int previousIndex = currentRecordIndex;
                currentRecordIndex += [tmpArray count];
                
                if ([tmpArray count] == NUMBER_ROW_PAGE) {
                    isLoadMore = YES;
                }
                else {
                    isLoadMore = NO;
                }
                
                if (isLoadMore) {
                    //                    [self.mainTableView showLoadingMore];
                }
                else
                    [self.tableView hideLoadingMore];
                [self.tableView hideActivity];
                
                NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
                for (int i=previousIndex;i < currentRecordIndex;i++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                    [indexPaths addObject:indexPath];
                }
                if ([indexPaths count] > 0) {
                    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                
                isLock = NO;
//            });
//        });
#if !OS_OBJECT_USE_OBJC
//        dispatch_release(myQueue);
#endif
//        myQueue = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark scrollview load more
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    didReadyLoadMore = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height && !didReadyLoadMore)
    {
        didReadyLoadMore = YES;
        
        if(!isLoadMore)
        {
            [self.tableView hideLoadingMore];
            return;
        }
        switch (filterType) {
            case filter_room:
                [self listMoreRoom];
                break;
            case filter_person_in_charge:
                [self listMoreContact];
                break;
            case filter_building:
                [self listMoreBuildings];
                break;
            case filter_location:
                [self listMoreOtherBuildings];
                break;
            default:
                break;
        }
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [mainArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 21)];
        lbTitle.tag = 100;
        lbTitle.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lbTitle];
    }
    if (indexPath.row == 0 && filterType == filter_liaison) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    UILabel *lbTitle = (UILabel*)[cell.contentView viewWithTag:100];
    
    NSMutableDictionary *filterDict = [mainArray objectAtIndex:indexPath.row];
    lbTitle.text = [filterDict objectForKey:@"filterName"];
    
    if (filterType == filter_maintenance || filterType == filter_scheduleCategory) {
        NSMutableDictionary *filterDict = [mainArray objectAtIndex:indexPath.row];
        int level = [[filterDict objectForKey:@"level"] intValue];
        if (level > 0)
            lbTitle.frame = CGRectMake(30, 10, 250, 21);
        //if select PMWS or AWS, do nothing
        int maintenanceType = [[filterDict objectForKey:@"index"] intValue];
        if (maintenanceType == PMWS || maintenanceType == AWS)
            cell.userInteractionEnabled = NO;
    }
    
    //set checkmark for selected row
    if (filterType == filter_maintenance && [delegate isKindOfClass:[MaintenanceMgmtViewController class]]){
        if (currentSelectedIndex == [[filterDict objectForKey:@"index"] intValue])
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0 && (filterType == filter_liaison || filterType == filter_category))
        return;
    if ([delegate respondsToSelector:@selector(didSelectFilter:)]) {
        NSMutableDictionary *filterDict = [mainArray objectAtIndex:indexPath.row];
        [filterDict setObject:[NSNumber numberWithInt:filterType] forKey:@"filterType"];
        [delegate didSelectFilter:filterDict];
    }
}

@end
